package cn.zlp.hrm.service.impl;

import cn.zlp.hrm.client.LoginUserClient;
import cn.zlp.hrm.client.ThirdPartyServerClient;
import cn.zlp.hrm.domain.LoginUser;
import cn.zlp.hrm.domain.VipBase;
import cn.zlp.hrm.domain.VipUser;
import cn.zlp.hrm.dto.RegisterDto;
import cn.zlp.hrm.exception.ValidUtils;
import cn.zlp.hrm.mapper.VipBaseMapper;
import cn.zlp.hrm.mapper.VipUserMapper;
import cn.zlp.hrm.service.IVipBaseService;
import cn.zlp.hrm.util.AjaxResult;
import cn.zlp.hrm.util.UserConstants;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author zlp
 * @since 2022-01-13
 */
@Service
public class VipBaseServiceImpl extends ServiceImpl<VipBaseMapper, VipBase> implements IVipBaseService {

    @Autowired
    private ThirdPartyServerClient thirdPartyServerClient;

    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Autowired
    private VipUserMapper vipUserMapper;

    /**
     * 短信验证码注册
     * @param registerDto
     * @return
     */
    @Override
    // @GlobalTransactional(rollbackFor = Exception.class)
    @GlobalTransactional // 默认(rollbackFor = Exception.class)
    public AjaxResult register(RegisterDto registerDto) {
        // 1.校验 null+短信验证码+用户检验
        // 1.1 null校验省略
        // 1.2断行验证码校验调用feigen接口
        Map<String,String> params = new HashMap<>();
        params.put("key", UserConstants.REGISTER_CODE_PREFIX + registerDto.getMobile());
        params.put("code",registerDto.getSmsCode());
        // 调用方法
        AjaxResult result = thirdPartyServerClient.verifySmsCode(params);
        ValidUtils.assertTrue(result.isSuccess(),result.getMessage());
        // 2.远程调用hrm-auth保存loginUser并获取到loginId
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(registerDto.getMobile());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String enPass = bCryptPasswordEncoder.encode(registerDto.getPassword());
        loginUser.setPassword(enPass);
        loginUser.setType(1);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        ValidUtils.assertTrue(ajaxResult.isSuccess(),ajaxResult.getMessage());
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString()) ;
        //3 保存vipBase，vipuser
        VipBase vipBase = new VipBase();
        vipBase.setPassword(enPass);
        vipBase.setPhone(registerDto.getMobile());
        vipBase.setNickName(registerDto.getMobile());
        vipBase.setBitState(18L);
        vipBase.setLoginId(loginId);
        vipBaseMapper.insert(vipBase);

        VipUser vipUser = new VipUser();
        vipUser.setVipId(vipBase.getId());
        vipUserMapper.insert(vipUser);
        return AjaxResult.me();
    }
}
